package com.health.fragment.focusgroup;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.health.model.PeopleType;
import com.health.tablebean.webservice.HdDiseaseManageStroke;
import com.health.tablebean.webservice.HdPersonalPharmacy;
import com.health.tablebean.webservice.HdPersonalRollOut;
import com.health.viewscontrols.FWMYDPJActivity;
import com.health.viewscontrols.NovaApplication;
import com.health.viewscontrols.PersonalFileActivity.onFragmentSave;
import com.health.R;
import com.health.adapter.CheckBoxAdapter;
import com.health.adapter.RadioButtonAdapter;
import com.health.db.FDbManager;
import com.health.viewscontrols.focusgroup.GXYActivity;
import com.health.viewscontrols.focusgroup.NCZActivity;
import com.health.fragment.NovaFragment;
import com.health.tablebean.ARCHIVE_BASEINFO;
import com.health.tablebean.BD_DD_INFO;
import com.health.tablebean.CD_DRUGCONDITION;
import com.health.tablebean.CD_STROKE_FOLLOWUP;
import com.health.tools.GlobalConts;
import com.health.view.spinner.NiceSpinner;
import com.health.widget.EditTextWithDate;
import com.health.widget.MyGridView;
import com.novabluetooth.services.BluetoothConstant;
import com.novabluetooth.views.ExamHistoryActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;

/**
 * 重点人群-- 脑卒中---随访
 */
public class NCZ_SfFragment extends NovaFragment implements onFragmentSave, OnClickListener {
    private static final String TAG = "NCZ_SfFragment";
    private View cqsf2View = null;
    private FDbManager fdbmanger = null;

    // ------------------------------基本信息-----------------------------
    private EditText ed_xm;

    private EditTextWithDate ed_date;
    private NiceSpinner ed_sfys;// 随访医生
    private NiceSpinner nice_spinner_szfs;// 选择随诊方式

    private MyGridView gd_zz;// 症状

    private EditText ed_xy;// 血压
    private Button btn_choose_xy;

    private EditText ed_tz;// 体重
    private Button btn_choose_tz;// 选择体重

    private MyGridView gd_czzd;//卒中诊断
    private EditText ed_height;// 身高

    private EditText ed_tz_target;// 目标体重

    private EditText ed_tzzs_target;// 目标体质指数

    private EditText ed_tzzs;// 体质指数

    private EditText ed_rate;// 心率

    private EditText ed_other;// 其他症状

    private MyGridView gd_day_smoking; //日吸烟量(支)

    private EditText ed_number_smoking; // 日吸烟量

    private EditText ed_number_targetsmoking; // 目标日吸烟量

    private MyGridView gd_day_driking; //日饮酒量(两)

    private EditText ed_number_driking; // 日饮酒量

    private EditText ed_number_targetdriking; // 目标日饮酒量

    private EditText ed_number_everyWeekend;//运动 次/周

    private EditText ed_minute_everyTime;//运动 分钟/次

    private EditText ed_target_number_everyWeekend;//目标运动 次/周

    private EditText ed_target_minute_everyTime;//目标运动 分钟/次

    private MyGridView gd_salt;//摄盐情况:

    private MyGridView gd_targetSalt;//目标摄盐情况:

    private MyGridView gd_heart;//心理调整:

    private EditText ed_xt;//血糖
    private EditText ed_ztzaqk;//肢体障碍情况
    private EditText ed_speak;//语言功能


    private NiceSpinner nice_spinner_zyxw;// 遵医行为

    private EditText ed_fzjc;// 辅助检查
    private NiceSpinner nice_spinner_fyycx;// 服药依从性
    private MyGridView gd_ywblfy;// 药物不良反应

    private NiceSpinner nice_spinner_clsffl;// 此类随访分类

    private EditText ed_yw1;// 药物名称1

    private EditText ed_yw1_mr;// 每日 day
    private EditText ed_yw1_mcjl;// 每次 mg

    private EditText ed_yw2;// 药物名称1

    private EditText ed_yw2_mr;// 每日 day
    private EditText ed_yw2_mcjl;// 每次 mg

    private EditText ed_yw3;// 药物名称1

    private EditText ed_yw3_mr;// 每日 day
    private EditText ed_yw3_mcjl;// 每次 mg

    private EditText ed_yw4;// 药物名称1

    private EditText ed_yw4_mr;// 每日 day
    private EditText ed_yw4_mcjl;// 每次 mg

    private EditText ed_zzyy;// 转诊原因
    private EditText ed_jgkb;// 机构及科别
    private EditTextWithDate ed_xcsfsj;// 下次随访时间
    private NiceSpinner nice_spinner_dw1;// 药物单位1
    private NiceSpinner nice_spinner_dw2;// 药物单位2
    private NiceSpinner nice_spinner_dw3;// 药物单位3
    private NiceSpinner nice_spinner_dwqt;// 药物单位其他
    // ----------------------------------------------------------------------

    private RadioButtonAdapter adapter_ywblfy = null;
    private CheckBoxAdapter adapter_czzd = null;//卒中诊断
    private RadioButtonAdapter adapter_smoking = null;
    private RadioButtonAdapter adapter_driking = null;
    private RadioButtonAdapter adapter_salt = null;
    private RadioButtonAdapter adapter_targetsalt = null;
    private RadioButtonAdapter adapter_heart = null;
    private CheckBoxAdapter adapter_zz = null;

    private List<BD_DD_INFO> szfs_list = new ArrayList<BD_DD_INFO>();
    private List<BD_DD_INFO> xltz_list = new ArrayList<BD_DD_INFO>();
    private List<BD_DD_INFO> zyxw_list = new ArrayList<BD_DD_INFO>();
    private List<BD_DD_INFO> fyycx_list = new ArrayList<BD_DD_INFO>();

    private List<BD_DD_INFO> ccsffl_list = new ArrayList<BD_DD_INFO>();
    private List<BD_DD_INFO> ywdw_list = new ArrayList<BD_DD_INFO>();

    private String str_ywblfyContent = "";
    private String str_ywblfyId = "";
    private String str_zzContent = "";
    private String str_zzId = "";

    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    public ARCHIVE_BASEINFO userInfo;
    private String uuid = "";
    private int from_followup = -1;
    private int from_drug = -1;
    private String drug_id1 = "";
    private String drug_id2 = "";
    private String drug_id3 = "";
    private String drug_id4 = "";

    @Override
    public View findViews(LayoutInflater inflater) {
        fdbmanger = NovaApplication.fdbmanger;
        userInfo = NovaApplication.getUserInfo();
        cqsf2View = inflater.inflate(R.layout.ncz_sfxx, null);
        initViews(cqsf2View);
        GlobalConts.setMaxLength(13, ed_xy);
        GlobalConts.setMaxLength(6, ed_tz);
        GlobalConts.keep2decimal(ed_tz);
        GlobalConts.setMaxLength(50, ed_fzjc, ed_zzyy, ed_jgkb);
        GlobalConts.setMaxLength(25, ed_yw1, ed_yw2, ed_yw3, ed_yw4);
        GlobalConts.setMaxLength(3, ed_yw1_mr, ed_yw2_mr, ed_yw3_mr, ed_yw4_mr);
        GlobalConts.setMaxLength(6, ed_yw1_mcjl, ed_yw2_mcjl, ed_yw3_mcjl, ed_yw4_mcjl);// 每次
        return cqsf2View;
    }

    /**
     * 初始化布局
     *
     * @param v
     * @author 王婷婷
     * @date 2015年12月21日 上午11:10:55
     */
    private void initViews(View v) {

        ed_xm = (EditText) v.findViewById(R.id.ed_xm);
        ed_date = (EditTextWithDate) v.findViewById(R.id.ed_date);
        ed_sfys = (NiceSpinner) v.findViewById(R.id.ed_sfys);
        nice_spinner_szfs = (NiceSpinner) v.findViewById(R.id.nice_spinner_szfs);

        gd_zz = (MyGridView) v.findViewById(R.id.gd_zz);

        ed_xy = (EditText) v.findViewById(R.id.ed_xy);
        btn_choose_xy = (Button) v.findViewById(R.id.btn_choose_xy);

        ed_tz = (EditText) v.findViewById(R.id.ed_tz);
        btn_choose_tz = (Button) v.findViewById(R.id.btn_choose_tz);


        gd_czzd = (MyGridView) v.findViewById(R.id.gd_czzd);//  卒中
        ed_height = (EditText) v.findViewById(R.id.ed_height);//  身高
        ed_tz_target = (EditText) v.findViewById(R.id.ed_tz_target);//   目标体重
        ed_tzzs_target = (EditText) v.findViewById(R.id.ed_tzzs_target);//  目标体质指标
        ed_tzzs = (EditText) v.findViewById(R.id.ed_tzzs);//  体质指标
        ed_rate = (EditText) v.findViewById(R.id.ed_rate);//  心率
        ed_other = (EditText) v.findViewById(R.id.ed_other);//其他体征
        gd_day_smoking = (MyGridView) v.findViewById(R.id.gd_day_smoking);////日吸烟量(支)
        ed_number_smoking = (EditText) v.findViewById(R.id.ed_number_smoking);////日吸烟量(支)
        ed_number_targetsmoking = (EditText) v.findViewById(R.id.ed_number_targetsmoking);////目标日吸烟量(支)
        gd_day_driking = (MyGridView) v.findViewById(R.id.gd_day_driking);//日饮酒量(两)
        ed_number_driking = (EditText) v.findViewById(R.id.ed_number_driking);//日饮酒量(两)
        ed_number_targetdriking = (EditText) v.findViewById(R.id.ed_number_targetdriking);//目标日饮酒量(两)
        ed_number_everyWeekend = (EditText) v.findViewById(R.id.ed_number_everyWeekend);//运动 次/周
        ed_minute_everyTime = (EditText) v.findViewById(R.id.ed_minute_everyTime);//运动 分钟/次
        ed_target_number_everyWeekend = (EditText) v.findViewById(R.id.ed_target_number_everyWeekend);//目标运动 次/周
        ed_target_minute_everyTime = (EditText) v.findViewById(R.id.ed_target_minute_everyTime);//目标运动  分钟/次
        gd_salt = (MyGridView) v.findViewById(R.id.gd_salt);//摄盐情况:
        gd_targetSalt = (MyGridView) v.findViewById(R.id.gd_targetSalt);//目标摄盐情况:
        gd_heart = (MyGridView) v.findViewById(R.id.gd_heart);//心理调整:
        ed_xt = (EditText) v.findViewById(R.id.ed_xt);//血糖:
        ed_ztzaqk = (EditText) v.findViewById(R.id.ed_ztzaqk);//肢体障碍情况
        ed_speak = (EditText) v.findViewById(R.id.ed_speak);//语言功能


        nice_spinner_zyxw = (NiceSpinner) v.findViewById(R.id.nice_spinner_zyxw);
        ed_fzjc = (EditText) v.findViewById(R.id.ed_fzjc);
        nice_spinner_fyycx = (NiceSpinner) v.findViewById(R.id.nice_spinner_fyycx);
        gd_ywblfy = (MyGridView) v.findViewById(R.id.gd_ywblfy);

        nice_spinner_clsffl = (NiceSpinner) v.findViewById(R.id.nice_spinner_clsffl);

        ed_yw1 = (EditText) v.findViewById(R.id.ed_yw1);
        ed_yw1_mr = (EditText) v.findViewById(R.id.ed_yw1_mr);

        ed_yw1_mcjl = (EditText) v.findViewById(R.id.ed_yw1_mcjl);

        ed_yw2 = (EditText) v.findViewById(R.id.ed_yw2);
        ed_yw2_mr = (EditText) v.findViewById(R.id.ed_yw2_mr);

        ed_yw2_mcjl = (EditText) v.findViewById(R.id.ed_yw2_mcjl);

        ed_yw3 = (EditText) v.findViewById(R.id.ed_yw3);
        ed_yw3_mr = (EditText) v.findViewById(R.id.ed_yw3_mr);

        ed_yw3_mcjl = (EditText) v.findViewById(R.id.ed_yw3_mcjl);

        ed_yw4 = (EditText) v.findViewById(R.id.ed_yw4);
        ed_yw4_mr = (EditText) v.findViewById(R.id.ed_yw4_mr);

        ed_yw4_mcjl = (EditText) v.findViewById(R.id.ed_yw4_mcjl);

        ed_zzyy = (EditText) v.findViewById(R.id.ed_zzyy);
        ed_jgkb = (EditText) v.findViewById(R.id.ed_jgkb);
        ed_xcsfsj = (EditTextWithDate) v.findViewById(R.id.ed_xcsfsj);
        nice_spinner_dw1 = (NiceSpinner) v.findViewById(R.id.nice_spinner_dw1);
        nice_spinner_dw2 = (NiceSpinner) v.findViewById(R.id.nice_spinner_dw2);
        nice_spinner_dw3 = (NiceSpinner) v.findViewById(R.id.nice_spinner_dw3);
        nice_spinner_dwqt = (NiceSpinner) v.findViewById(R.id.nice_spinner_dwqt);
        ed_date.setBefore(true);
        ed_xcsfsj.setAfter(true);
        GlobalConts.doctorList(ed_sfys);
    }

    /**
     * 绑定数据值
     *
     * @param str_where
     * @param flag
     * @author 王婷婷
     * @date 2015年12月17日 上午10:30:30
     */
    private void bindDdata(String flag, String str_where) {
        List<BD_DD_INFO> listZZs = fdbmanger.findAllByWhereInInfoDB(BD_DD_INFO.class, str_where);

        if (flag.equals("症状")) {
            adapter_zz = new CheckBoxAdapter(getActivity(), listZZs);
            adapter_zz.setOtherMaxLength(50);
            gd_zz.setAdapter(adapter_zz);
        } else if (flag.equals("卒中诊断")) {
            adapter_czzd = new CheckBoxAdapter(getActivity(), listZZs);
            adapter_czzd.setOtherMaxLength(50);
            gd_czzd.setAdapter(adapter_czzd);
        }else if (flag.equals("药物不良反应")) {
            adapter_ywblfy = new RadioButtonAdapter(listZZs, getActivity());
            adapter_ywblfy.setOtherMaxLength(50);
            gd_ywblfy.setAdapter(adapter_ywblfy);
        }else if("是否吸烟".equals(flag)){
            adapter_smoking = new RadioButtonAdapter(listZZs, getActivity());
            adapter_smoking.setOtherTextVisible(false);
            gd_day_smoking.setAdapter(adapter_smoking);
            adapter_smoking.setCheckedChengeLinstener(new RadioButtonAdapter.RBACheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    String check = adapter_smoking.getChecked();
//                    if("DM01-10_1".equals(check)){//当前getChecked的是
                    if("是".equals(buttonView.getText().toString())){
                        ed_number_smoking.setEnabled(true);
                        ed_number_targetsmoking.setEnabled(true);
                        ed_number_smoking.requestFocus();
                        ed_number_smoking.setText("");
                        ed_number_targetsmoking.setText("");
                    }else if ("否".equals(buttonView.getText().toString())){
                        ed_number_smoking.setText("0");
                        ed_number_targetsmoking.setText("0");
                        ed_number_smoking.setEnabled(false);
                        ed_number_targetsmoking.setEnabled(false);
                        ed_number_smoking.clearFocus();
                        ed_number_targetsmoking.clearFocus();
                    }
                }
            });
        }else if("是否饮酒".equals(flag)){
            adapter_driking = new RadioButtonAdapter(listZZs, getActivity());
            adapter_driking.setOtherTextVisible(false);
            gd_day_driking.setAdapter(adapter_driking);
            adapter_driking.setCheckedChengeLinstener(new RadioButtonAdapter.RBACheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    String check = adapter_driking.getChecked();
                    if("是".equals(buttonView.getText().toString())){//当前getChecked的是
                        ed_number_driking.setEnabled(true);
                        ed_number_targetdriking.setEnabled(true);
                        ed_number_driking.requestFocus();
                        ed_number_driking.setText("");
                        ed_number_targetdriking.setText("");
                    }else if ("否".equals(buttonView.getText().toString())){
                        ed_number_driking.setText("0");
                        ed_number_targetdriking.setText("0");
                        ed_number_driking.setEnabled(false);
                        ed_number_targetdriking.setEnabled(false);
                        ed_number_driking.clearFocus();
                        ed_number_targetdriking.clearFocus();
                    }
                }
            });
        }else if("摄盐情况".equals(flag)){
            adapter_salt = new RadioButtonAdapter(listZZs, getActivity());
            adapter_salt.setOtherTextVisible(false);
            gd_salt.setAdapter(adapter_salt);

        }else if("目标摄盐情况".equals(flag)){
            adapter_targetsalt = new RadioButtonAdapter(listZZs, getActivity());
            adapter_targetsalt.setOtherTextVisible(false);
            gd_targetSalt.setAdapter(adapter_targetsalt);
        }else if("心理调整".equals(flag)){
            int size = listZZs.size();
            List<BD_DD_INFO> list= new ArrayList<BD_DD_INFO>();
            if (size > 0) {
                for (int i = 0; i < size; i++) {
                    String str_id = listZZs.get(i).getID();
                    if (str_id.equals("DM01-69_1") || str_id.equals("DM01-69_2") || str_id.equals("DM01-69_3")
                            || str_id.equals("")) {
                        list.add(listZZs.get(i));
                    }
                }
            }
            adapter_heart = new RadioButtonAdapter(list, getActivity());
            adapter_heart.setOtherTextVisible(false);
            gd_heart.setAdapter(adapter_heart);
        }
    }

    @Override
    public void setupView(View view) {
        // TODO Auto-generated method stub

    }

    @Override
    public void addListener() {
        btn_choose_xy.setOnClickListener(this);
        btn_choose_tz.setOnClickListener(this);
        changeBMI(ed_height, ed_tz, ed_tzzs);
        changeBMI(ed_height, ed_tz_target, ed_tzzs_target);
    }
    /**
     * 身高或者体重改变时，BMI的值随之发生改变
     **/
    private void changeBMI(final EditText etHeight, final EditText etWeight, final EditText etBMI) {
        etHeight.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(etWeight.getText().toString())) {
                    setBMI(etHeight, etWeight, etBMI);
                }else{
                    etBMI.setText("");
                }

                if (TextUtils.isEmpty(s.toString())) {
                    etBMI.setText("");
                }

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        etWeight.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(etHeight.getText().toString())) {
                    setBMI(etHeight, etWeight, etBMI);
                }else{
                    etBMI.setText("");
                }
                if (TextUtils.isEmpty(s.toString())) {
                    etBMI.setText("");
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }
    /**
     * 设置BMI
     *
     * @author ewenqiang
     * @date 2017年4月5日 上午10:48:51
     */
    private void setBMI(EditText etHeight, EditText etWeight, EditText etBMI) {
        try {
            if (TextUtils.isEmpty(etHeight.getText().toString()) || TextUtils.isEmpty(etHeight.getText().toString())) {
                etBMI.setText("");
                return;
            }
            double dh = Double.parseDouble(etHeight.getText().toString().trim()) / 100;
            double dw = Double.parseDouble(etWeight.getText().toString().trim());

            java.text.DecimalFormat df = new java.text.DecimalFormat("#.00");

            etBMI.setText(df.format(GlobalConts.getBMI(dh, dw)) + "");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    @Override
    public void setData() {
        try {
            bindDdata("卒中诊断", "PARENTID='DM06-07'");
            bindDdata("症状", "PARENTID='DM04-07'");
            bindDdata("药物不良反应", "PARENTID='DM01-11'");

            bindSpinner("随访方式", "PARENTID='DM04-04'");
            bindSpinner("遵医行为", "PARENTID='DM01-69'");
            bindSpinner("药物依从性", "PARENTID='DM01-70'");
            bindSpinner("此次随访分类", "PARENTID='DM01-71'");
            bindSpinner("药物单位", "PARENTID='DM04-05'");

            bindDdata("是否吸烟", "PARENTID='DM01-10'");
            bindDdata("是否饮酒", "PARENTID='DM01-10'");
            bindDdata("摄盐情况", "PARENTID='DM01-68'");
            bindDdata("目标摄盐情况", "PARENTID='DM01-68'");
            bindDdata("心理调整", "PARENTID='DM01-69'");

            setData_followUP();
            setData_drug();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 设置下拉
     *
     * @param flagName
     * @param strSql
     * @author 王婷婷
     * @date 2015年12月24日 上午9:19:28
     */

    private void bindSpinner(String flagName, String strSql) {
        List<BD_DD_INFO> sList = new ArrayList<BD_DD_INFO>();
        BD_DD_INFO bd = new BD_DD_INFO();
        bd.setDATAINFO("请选择");
        bd.setID("");
        sList.add(bd);

        List<BD_DD_INFO> sList1 = fdbmanger.findAllByWhereInInfoDB(BD_DD_INFO.class,
                strSql + " order by cast(SORT as int) asc");
        sList.addAll(sList1);
        if (flagName.equals("随访方式")) {
            // nice_adapter_pf = new NiceSpinnerAdapter<>(getActivity(), sList);
            // nice_spinner_pf.setAdapter(nice_adapter_pf);
            szfs_list = sList;
            nice_spinner_szfs.attachDataSource(szfs_list);

        } else if (flagName.equals("心理调整")) {
            int size = sList.size();
            if (size > 0) {
                xltz_list = new ArrayList<BD_DD_INFO>();
                zyxw_list = new ArrayList<BD_DD_INFO>();
                for (int i = 0; i < size; i++) {
                    String str_id = sList.get(i).getID();
                    if (str_id.equals("DM01-69_1") || str_id.equals("DM01-69_2") || str_id.equals("DM01-69_3")
                            || str_id.equals("")) {
                        xltz_list.add(sList.get(i));
                        zyxw_list.add(sList.get(i));
                    }
                }
            }
            nice_spinner_zyxw.attachDataSource(zyxw_list);

        } else if (flagName.equals("药物依从性")) {
            // nice_adapter_lbj = new NiceSpinnerAdapter<>(getActivity(),
            // sList);
            fyycx_list = sList;
            nice_spinner_fyycx.attachDataSource(fyycx_list);

        } else if (flagName.equals("此次随访分类")) {
            int size = sList.size();
            if (size > 0) {
                ccsffl_list = new ArrayList<BD_DD_INFO>();
                for (int i = 0; i < size; i++) {
                    String str_id = sList.get(i).getID();
                    if (str_id.equals("DM01-71_1") || str_id.equals("DM01-71_2") || str_id.equals("DM01-71_3")
                            || str_id.equals("DM01-71_4") || str_id.equals("")) {
                        ccsffl_list.add(sList.get(i));
                    }
                }
            }

            nice_spinner_clsffl.attachDataSource(ccsffl_list);
        } else if (flagName.equals("遵医行为")) {

            int size = sList.size();
            if (size > 0) {
                zyxw_list = new ArrayList<BD_DD_INFO>();
                for (int i = 0; i < size; i++) {
                    String str_id = sList.get(i).getID();
                    if (str_id.equals("DM01-69_1") || str_id.equals("DM01-69_2") || str_id.equals("DM01-69_3")
                            || str_id.equals("")) {
                        zyxw_list.add(sList.get(i));
                    }
                }
            }

            nice_spinner_zyxw.attachDataSource(zyxw_list);

        }else if (flagName.equals("药物单位")) {
            ywdw_list = sList1;
            nice_spinner_dw1.attachDataSource(sList1);
            nice_spinner_dw2.attachDataSource(sList1);
            nice_spinner_dw3.attachDataSource(sList1);
            nice_spinner_dwqt.attachDataSource(sList1);
        }

    }

    @Override
    public void save() {
        // 随诊方式
        String str_id = szfs_list.get(nice_spinner_szfs.getSelectedIndex()).getID();
        Log.w(TAG, "随诊方式：" + str_id);
        if (TextUtils.isEmpty(str_id)) {
            nice_spinner_szfs.setBackgroundColor(Color.YELLOW);
            ((NCZActivity) getActivity()).showTextDialog("请选择随诊方式");
            return;
        } else {
            nice_spinner_szfs.setBackgroundColor(Color.WHITE);
        }

        // 卒中诊断
        String str_czzd = adapter_czzd.getCheckedIdsStr();
        if (TextUtils.isEmpty(str_czzd)) {
            gd_czzd.setBackgroundColor(Color.YELLOW);
            ((NCZActivity) getActivity()).showTextDialog("请选择卒中诊断");
            return;
        } else {
            gd_czzd.setBackgroundColor(Color.WHITE);
        }

        // 症状
        String str_zz = adapter_zz.getCheckedIdsStr();
        if (TextUtils.isEmpty(str_zz)) {
            gd_zz.setBackgroundColor(Color.YELLOW);
            ((NCZActivity) getActivity()).showTextDialog("请选择症状");
            return;
        } else {
            gd_zz.setBackgroundColor(Color.WHITE);
        }

        // 血压
        String str_xy = ed_xy.getText().toString();
        if (TextUtils.isEmpty(str_xy)) {
            ed_xy.setBackgroundColor(Color.YELLOW);
            ((NCZActivity) getActivity()).showTextDialog("请填写血压");
            return;
        } else {
            if (!str_xy.contains("/")) {
                ed_xy.setBackgroundColor(Color.YELLOW);
                ((NCZActivity) getActivity()).showTextDialog("血压数据不合理，保存失败");
                return;
            }
            ed_xy.setBackgroundColor(Color.WHITE);
        }

        if(!editCheck(ed_tz,"身高",300)){
            return;
        }

        if(!editCheck(ed_tz,"体重",200)){
            return;
        }

        if(!editCheck(ed_tz_target,"目标体重",200)){
            return;
        }
        if(!editCheck(ed_tzzs,"体质指数",100)){
            return;
        }
        if(!editCheck(ed_tzzs_target,"目标体质指数",100)){
            return;
        }
        if(!editCheck(ed_rate,"心率",200)){
            return;
        }
        if("".equals(adapter_smoking.getChecked())){
            ((NCZActivity) getActivity()).showTextDialog("请选择是否吸烟");
            return;
        }
        if("".equals(adapter_driking.getChecked())){
            ((NCZActivity) getActivity()).showTextDialog("请选择是否饮酒");
            return;
        }

        if(!editCheck(ed_number_smoking,"日吸烟量",100)){
            return;
        }
        if(!editCheck(ed_number_targetsmoking,"目标日吸烟量",100)){
            return;
        }
        if(!editCheck(ed_number_driking,"日饮酒量",100)){
            return;
        }
        if(!editCheck(ed_number_targetdriking,"目标日饮酒量",100)){
            return;
        }
        if(!editCheck(ed_number_everyWeekend,"运动次数",9)){
            return;
        }
        if(!editCheck(ed_minute_everyTime,"运动时间",999)){
            return;
        }
        if(!editCheck(ed_target_number_everyWeekend,"目标运动次数",9)){
            return;
        }
        if(!editCheck(ed_target_minute_everyTime,"目标运动时间",999)){
            return;
        }

        // 下次随访时间
        String str_xcsfsj = ed_xcsfsj.getText().toString();
        if (TextUtils.isEmpty(str_xcsfsj)) {
            ed_xcsfsj.setBackgroundColor(Color.YELLOW);
            ((NCZActivity) getActivity()).showTextDialog("请选择下次随访时间");
            return;
        } else {
            ed_xcsfsj.setBackgroundColor(Color.WHITE);
        }
        getRadioButtonCheckedData();
        getCheckBoxedData();
        saveData_followup();
        saveDrug();
    }
    private boolean editCheck(EditText edit,String title,int limit){
        String str = edit.getText().toString();
        if (TextUtils.isEmpty(str)) {
            edit.setBackgroundColor(Color.YELLOW);
            ((NCZActivity) getActivity()).showTextDialog("请输入" + title);
            return false;
        } else {
            if(limit == 0){
                edit.setBackgroundColor(Color.WHITE);
                return true;
            }
            if (Double.valueOf(str) > limit) {
                edit.setBackgroundColor(Color.YELLOW);
                ((NCZActivity) getActivity()).showTextDialog(title+"最大值为"+limit);
                return false;
            }
            edit.setBackgroundColor(Color.WHITE);
            return true;
        }
    }
    /**
     * 获取转诊的信息
     *
     * @author 王婷婷
     * @date 2015年12月16日 下午3:10:48
     */
    private void getRadioButtonCheckedData() {

        str_ywblfyContent = adapter_ywblfy.getCheckedOther();
        str_ywblfyId = adapter_ywblfy.getChecked();

    }

    /**
     * 获取 被选中的checkbox
     *
     * @author 王婷婷
     * @date 2015年12月19日 上午11:34:30
     */
    private void getCheckBoxedData() {
        str_zzId = adapter_zz.getCheckedIdsStr();
        str_zzContent = adapter_zz.getOther();
    }

    /**
     * 保存数据
     *
     * @author 王婷婷
     * @date 2015年12月17日 上午11:13:39
     */
    private void saveData_followup() {
        HdDiseaseManageStroke ques = new HdDiseaseManageStroke();
//		ques.setQdqxz();
//		ques.setWzd();
        ques.setPersonalid("");//档案编号
        ques.setCardid(userInfo.getIDCARD());
        ques.setStrokeDiagnose(adapter_czzd.getCheckedIdsStr());//卒中诊断
        ques.setOtherDiagnose(adapter_czzd.getOther());
        ques.setZtzaqk(ed_ztzaqk.getText().toString().trim());//肢体障碍情况
        ques.setYygn(ed_speak.getText().toString().trim());//肢体障碍情况
//		ques.setCoronaryCardid();
//		ques.setResDoctor();
//		ques.setVisitDate();
        ques.setVisitWay(szfs_list.get(nice_spinner_szfs.getSelectedIndex()).getID());//随访方式
        ques.setCurSymptom(str_zzId);//症状
        ques.setOtherSymptom(str_zzContent);//其他症状
//		ques.setStature();
        ques.setRes0(adapter_smoking.getChecked());
        ques.setRes1(adapter_driking.getChecked());
        ques.setXt(ed_xt.getText().toString().trim());
        ques.setAvoirdupois(ed_tz.getText().toString().trim());
        ques.setTargetAvoirdupois(ed_tz_target.getText().toString().trim());
        ques.setPhysique(ed_tzzs.getText().toString().trim());//体质指数
        ques.setTargetPhysique(ed_tzzs_target.getText().toString().trim());// 目标体质指数
//		ques.setHip();
//		ques.setWaostline();
        String str_xy = ed_xy.getText().toString().trim();
        if (str_xy.contains("/")) {
            String[] weight = str_xy.split("/");
            if(weight.length == 2){
                ques.setPressureH(weight[0]);// 血压 收缩压
                ques.setPressureL(weight[1]);// 血压 舒张压
            }
        }
        ques.setPulseRate(ed_rate.getText().toString().trim());
        ques.setOtherPositive(ed_other.getText().toString().trim());
        ques.setSmoke(ed_number_smoking.getText().toString().trim());
        ques.setTargetSmoke(ed_number_targetsmoking.getText().toString().trim());
        ques.setDrink(ed_number_driking.getText().toString().trim());
        ques.setTargetDrink(ed_number_targetdriking.getText().toString().trim());
        ques.setTraining(ed_number_everyWeekend.getText().toString().trim());
        ques.setTargetTraining(ed_target_number_everyWeekend.getText().toString().trim());
        ques.setTrainingMin(ed_minute_everyTime.getText().toString().trim());
        ques.setTargetTrainingMin(ed_target_minute_everyTime.getText().toString().trim());
        ques.setSalt(adapter_salt.getChecked());
        ques.setTargetSalt(adapter_targetsalt.getChecked());
        ques.setMentality(adapter_heart.getChecked());//心里调整
        ques.setCompiance(zyxw_list.get(nice_spinner_zyxw.getSelectedIndex()).getID());//遵医行为
        ques.setSubCheck(ed_fzjc.getText().toString().trim());//其他辅助检查
        ques.setDependence(fyycx_list.get(nice_spinner_fyycx.getSelectedIndex()).getID());//服药依从性
        ques.setSideEffects(str_ywblfyId);//药物不良反应
        ques.setEffectsState(str_ywblfyContent);//药物不良反应的详情
        ques.setVisitType(ccsffl_list.get(nice_spinner_clsffl.getSelectedIndex()).getID());//此次随访分类
//		ques.setCmType();
        ques.setNextVisitDate(ed_xcsfsj.getText().toString().trim());//下次随访日期
//		ques.setInputOrgancode();
//		ques.setInputOrgan();
//		ques.setInputIdcard();
        ques.setInputMan(NovaApplication.getDoctorList().get(ed_sfys.getSelectedIndex()).getUSER_ID());
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        ques.setIS_UPDATE("Y");
        ques.setHappentime(ed_date.getText().toString().trim());//随访时间
        ques.setHeight(ed_height.getText().toString().trim());//身高


        HdPersonalRollOut hdPersonalRollOut = new HdPersonalRollOut();
        hdPersonalRollOut.setHpc(ed_zzyy.getText().toString().trim());
        hdPersonalRollOut.setRollInRegion(ed_jgkb.getText().toString().trim());

        if (from_followup == 1) {
            fdbmanger.updata(hdPersonalRollOut,"item0='" + uuid+"'");
            ques.setRollout(hdPersonalRollOut);
            ques.setUpdatetime(formatter.format(curDate));
            ques.setUpdateorg(userInfo.getCREATEORG());
            ques.setModified(NovaApplication.getDoctorList().get(ed_sfys.getSelectedIndex()).getUSER_ID());
            fdbmanger.updata(ques, "cardid='" + userInfo.getIDCARD() + "'");
        } else {
            String uuid = GlobalConts.getUUID();
            hdPersonalRollOut.setItem0(uuid);
            ques.setRollout(hdPersonalRollOut);
            ques.setId(uuid);
            fdbmanger.save(hdPersonalRollOut);
            ques.setCreatetime(formatter.format(curDate));
            ques.setCreater(userInfo.getCREATER());
            ques.setCreateorg(userInfo.getCREATEORG());
            fdbmanger.save(ques);
            from_followup = 1;
        }
        if (GlobalConts.TYPE_JTYS.equals(NovaApplication.getType())) {
            new AlertDialog.Builder(getActivity()).setTitle("保存成功").setMessage("进行满意度评价")
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Intent intent = new Intent(getContext(), FWMYDPJActivity.class);
                            intent.putExtra("type", PeopleType.NCZ);
                            intent.putExtra("szfs", szfs_list.get(nice_spinner_szfs.getSelectedIndex()).getID());
                            startActivity(intent);
                        }
                    }).show();
        } else if (GlobalConts.TYPE_GJJBGGWS.equals(NovaApplication.getType())) {
            ((NCZActivity) getActivity()).showTextDialog("保存成功");
        }

    }

    private void saveDrug() {
        HdPersonalPharmacy drug1 = new HdPersonalPharmacy();
        drug1.setIDCARD(userInfo.getIDCARD());
        drug1.setTYPE("5");
        drug1.setDrugName(ed_yw1.getText().toString());
        drug1.setReg2(ywdw_list.get(nice_spinner_dw1.getSelectedIndex()).getID());
        String str_daytimes = ed_yw1_mr.getText().toString();
        drug1.setUsage(str_daytimes);
        String str_mcjl = ed_yw1_mcjl.getText().toString();
        drug1.setDosage(str_mcjl);


        HdPersonalPharmacy drug2 = new HdPersonalPharmacy();
        drug2.setIDCARD(userInfo.getIDCARD());
        drug2.setTYPE("5");
        drug2.setDrugName(ed_yw2.getText().toString());
        drug2.setReg2(ywdw_list.get(nice_spinner_dw2.getSelectedIndex()).getID());
        String str_daytimes2 = ed_yw2_mr.getText().toString();
        drug2.setUsage(str_daytimes2);
        String str_mcjl2 = ed_yw2_mcjl.getText().toString();
        drug2.setDosage(str_mcjl2);


        HdPersonalPharmacy drug3 = new HdPersonalPharmacy();
        drug3.setIDCARD(userInfo.getIDCARD());
        drug3.setTYPE("5");
        drug3.setDrugName(ed_yw3.getText().toString());
        drug3.setReg2(ywdw_list.get(nice_spinner_dw3.getSelectedIndex()).getID());
        String str_daytimes3 = ed_yw3_mr.getText().toString();
        drug3.setUsage(str_daytimes3);
        String str_mcjl3 = ed_yw3_mcjl.getText().toString();
        drug3.setDosage(str_mcjl3);


        HdPersonalPharmacy drug4 = new HdPersonalPharmacy();
        drug4.setIDCARD(userInfo.getIDCARD());
        drug4.setTYPE("5");
        drug4.setDrugName(ed_yw4.getText().toString());
        drug4.setReg2(ywdw_list.get(nice_spinner_dwqt.getSelectedIndex()).getID());
        String str_daytimes4 = ed_yw4_mr.getText().toString();
        drug4.setUsage(str_daytimes4);
        String str_mcjl4 = ed_yw4_mcjl.getText().toString();
        drug4.setDosage(str_mcjl4);

        if (from_drug == 1) {
            if ("".equals(drug_id1)) {
                fdbmanger.save(drug1);
            } else {
                fdbmanger.updata(drug1, "IDCARD='" + userInfo.getIDCARD() + "' and TYPE='5' and ID='" + drug_id1+"'");
            }
            if ("".equals(drug_id2)) {
                fdbmanger.save(drug2);
            } else {
                fdbmanger.updata(drug2, "IDCARD='" + userInfo.getIDCARD() + "' and TYPE='5' and ID='" + drug_id2+"'");
            }
            if ("".equals(drug_id3)) {
                fdbmanger.save(drug3);
            } else {
                fdbmanger.updata(drug3, "IDCARD='" + userInfo.getIDCARD() + "' and TYPE='5' and ID='" + drug_id3+"'");
            }
            if ("".equals(drug_id4)) {
                fdbmanger.save(drug4);
            } else {
                fdbmanger.updata(drug4, "IDCARD='" + userInfo.getIDCARD() + "' and TYPE='5' and ID='" + drug_id4+"'");
            }
        } else {
            drug_id1 = GlobalConts.getUUID();
            drug1.setID(drug_id1);
            drug_id2 = GlobalConts.getUUID();
            drug2.setID(drug_id2);
            drug_id3 = GlobalConts.getUUID();
            drug3.setID(drug_id3);
            drug_id4 = GlobalConts.getUUID();
            drug4.setID(drug_id4);
            fdbmanger.save(drug1);
            fdbmanger.save(drug2);
            fdbmanger.save(drug3);
            fdbmanger.save(drug4);
        }


    }

    /**
     * 设置 显示followup
     *
     * @author 王婷婷
     * @date 2015年12月24日 下午2:25:11
     */
    private void setData_followUP() {
        List<HdDiseaseManageStroke> queses = fdbmanger.findAllByWhere(HdDiseaseManageStroke.class,
                "cardid='" + userInfo.getIDCARD() + "'");
        int size = queses.size();
        if (size > 0) {

            from_followup = 1;

            ed_xm.setText(userInfo.getCUSTOMERNAME());

            HdDiseaseManageStroke ques1 = queses.get(0);

            uuid = ques1.getId();

            if (ques1.getHappentime() != null) {
                try {
                    ed_date.setText(ques1.getHappentime());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (ques1.getNextVisitDate() != null) {
                try {
                    ed_xcsfsj.setText(ques1.getNextVisitDate());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }


            adapter_czzd.setChecked(ques1.getStrokeDiagnose());
            adapter_czzd.setOther(ques1.getOtherDiagnose());
            ed_speak.setText(ques1.getYygn());
            ed_ztzaqk.setText(ques1.getZtzaqk());

            nice_spinner_szfs.setSelectedIndex(getListIndex(ques1.getVisitWay(), szfs_list));
            str_zzId = ques1.getCurSymptom();
            adapter_zz.setChecked(str_zzId);
            str_zzContent = ques1.getOtherSymptom();
            adapter_zz.setOther(str_zzContent);


            if (!TextUtils.isEmpty(ques1.getPressureH())) {
                String str_sy = ques1.getPressureH() + "/" + ques1.getPressureL();
                ed_xy.setText(str_sy);
            }
            ed_height.setText(ques1.getHeight());
            ed_tz.setText(ques1.getAvoirdupois());
            ed_tz_target.setText(ques1.getTargetAvoirdupois());
            ed_tzzs.setText(ques1.getPhysique());
            ed_tzzs_target.setText(ques1.getTargetPhysique());

            ed_xt.setText(ques1.getXt());

            ed_rate.setText(ques1.getPulseRate());
            ed_other.setText(ques1.getOtherPositive());
            adapter_smoking.setChecked(ques1.getRes0());
            if("DM01-10_1".equals(ques1.getRes0())){
                ed_number_smoking.setEnabled(false);
                ed_number_targetsmoking.setEnabled(false);
            }else{
                ed_number_smoking.setEnabled(true);
                ed_number_targetsmoking.setEnabled(true);
            }
            if("DM01-10_1".equals(ques1.getRes1())){
                ed_number_driking.setEnabled(false);
                ed_number_targetdriking.setEnabled(false);
            }else{
                ed_number_driking.setEnabled(true);
                ed_number_targetdriking.setEnabled(true);
            }

            ed_number_smoking.setText(ques1.getSmoke());
            ed_number_targetsmoking.setText(ques1.getTargetSmoke());
            adapter_driking.setChecked(ques1.getRes1());
            ed_number_driking.setText(ques1.getDrink());
            ed_number_targetdriking.setText(ques1.getTargetDrink());

            ed_number_everyWeekend.setText(ques1.getTraining());
            ed_minute_everyTime.setText(ques1.getTrainingMin());

            ed_target_number_everyWeekend.setText(ques1.getTargetTraining());
            ed_target_minute_everyTime.setText(ques1.getTargetTrainingMin());

            adapter_salt.setChecked(ques1.getSalt());
            adapter_targetsalt.setChecked(ques1.getTargetSalt());
            adapter_heart.setChecked(ques1.getMentality());

            nice_spinner_zyxw.setSelectedIndex(getListIndex(ques1.getCompiance(), zyxw_list));
            ed_fzjc.setText(ques1.getSubCheck());
            nice_spinner_fyycx.setSelectedIndex(getListIndex(ques1.getDependence(), fyycx_list));

            str_ywblfyId = ques1.getSideEffects();
            str_ywblfyContent = ques1.getEffectsState();
            adapter_ywblfy.setChecked(str_ywblfyId, str_ywblfyContent);

            nice_spinner_clsffl.setSelectedIndex(getListIndex(ques1.getVisitType(), ccsffl_list));

            List<HdPersonalRollOut> rollOuts = fdbmanger.findAllByWhere(HdPersonalRollOut.class,
                    "item0 ='" + ques1.getId() + "'");
            if(rollOuts.size() > 0){
                ed_zzyy.setText(rollOuts.get(0).getHpc());
                ed_jgkb.setText(rollOuts.get(0).getRollInRegion());
            }

            for (int i = 0; i < NovaApplication.getDoctorList().size(); i++) {
                if (ques1.getInputMan().equals(NovaApplication.getDoctorList().get(i).getUSER_ID())) {
                    ed_sfys.setSelectedIndex(i);
                }

            }

        } else {
            from_followup = 0;

            ed_xm.setText(userInfo.getCUSTOMERNAME());
            ed_date.setText(dateFormat.format(new Date()));
            for (int i = 0; i < NovaApplication.getDoctorList().size(); i++) {
                if (userInfo.getCREATED_BY().equals(NovaApplication.getDoctorList().get(i).getUSER_ID())) {
                    ed_sfys.setSelectedIndex(i);
                }

            }
        }
    }

    /**
     * 用药情况
     *
     * @author 王婷婷
     * @date 2015年12月24日 下午2:25:11
     */
    private void setData_drug() {
        List<HdPersonalPharmacy> queses = fdbmanger.findAllByWhere(HdPersonalPharmacy.class,
                "IDCARD='" + userInfo.getIDCARD() + "' and TYPE='5'");
        int size = queses.size();
        if (size > 0) {
            from_drug = 1;
            for (int i = 0; i < size; i++) {
                if (i == 0) {

                    HdPersonalPharmacy ques1 = queses.get(0);
                    drug_id1 = ques1.getID();
                    ed_yw1.setText(ques1.getDrugName());
                    ed_yw1_mr.setText(ques1.getUsage() + "");
                    ed_yw1_mcjl.setText(ques1.getDosage() + "");
                    nice_spinner_dw1.setSelectedIndex(getListIndex(ques1.getReg2(), ywdw_list));

                } else if (i == 1) {
                    HdPersonalPharmacy ques1 = queses.get(1);
                    drug_id2 = ques1.getID();
                    ed_yw2.setText(ques1.getDrugName());
                    ed_yw2_mr.setText(ques1.getUsage() + "");
                    ed_yw2_mcjl.setText(ques1.getDosage() + "");
                    nice_spinner_dw2.setSelectedIndex(getListIndex(ques1.getReg2(), ywdw_list));

                } else if (i == 2) {
                    HdPersonalPharmacy ques1 = queses.get(2);
                    drug_id3 = ques1.getID();
                    ed_yw3.setText(ques1.getDrugName());
                    ed_yw3_mr.setText(ques1.getUsage() + "");
                    ed_yw3_mcjl.setText(ques1.getDosage() + "");
                    nice_spinner_dw3.setSelectedIndex(getListIndex(ques1.getReg2(), ywdw_list));

                } else if (i == 3) {
                    HdPersonalPharmacy ques1 = queses.get(3);
                    drug_id4 = ques1.getID();
                    ed_yw4.setText(ques1.getDrugName());
                    ed_yw4_mr.setText(ques1.getUsage() + "");
                    ed_yw4_mcjl.setText(ques1.getDosage() + "");
                    nice_spinner_dwqt.setSelectedIndex(getListIndex(ques1.getReg2(), ywdw_list));

                }

            }

        } else {
            from_drug = 0;
        }
    }

    private int getListIndex(String str, List<BD_DD_INFO> bds) {
        if (str != null && !str.equals("")) {

            for (int i = 0; i < bds.size(); i++) {
                if (str.equals(bds.get(i).getID())) {
                    return i;
                }
            }
        }
        return 0;
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(getActivity(), ExamHistoryActivity.class);
        int id = v.getId();
        if (id == R.id.btn_choose_xy) {
            intent.putExtra("flag", BluetoothConstant.XUEYA_TYPENAME);
            getActivity().startActivityForResult(intent, GlobalConts.RequestXueya);
        } else if (id == R.id.btn_choose_tz) {
            intent.putExtra("flag", BluetoothConstant.TIZHONG_TYPENAME);
            getActivity().startActivityForResult(intent, GlobalConts.RequestTizhong);
        } else {
        }

    }

    @Override
    public void setTizhong(String value) {
        ed_tz.setText(value);
    }

    @Override
    public void setXueya(String gaoya, String diya, String mailv) {
        if (!TextUtils.isEmpty(gaoya) && !TextUtils.isEmpty(diya)) {
            ed_xy.setText(gaoya + "/" + diya);
        }
    }

    @Override
    public void setXueYang(String xueYang) {

    }
}
